import dayjs from "dayjs";
export function timeConversion(str) {
    let newStr = str.replace(/-/g, "/");
    let timeFormatter = str.substring(10, 16);
    //目标日期的年月日
    let targetDateStr = newStr.split(" ")[0];
    //当前的日期年月日
    let currentDateStr = getCurrentDate();

    if (targetDateStr === currentDateStr) {
        return `今日${timeFormatter}`;
    }
    //获取目标日期
    let targetMill = (new Date(newStr)).getTime();
    //当前的时间戳
    let currentMill = (new Date()).getTime();
    //两个时间戳相差的毫秒数
    let diffrentTimeMill = currentMill - targetMill;

    let diffrentDay = Math.ceil((diffrentTimeMill / 86400000));

    if (diffrentDay === 1) {
        return `昨日${timeFormatter}`;
    }
    return diffrentDay + "天前" + timeFormatter;
}

export function timeConvert(str) {
    if (str === undefined || str === null || str.length < 10) {
        return "";
    }
    let newStr = str.replace(/-/g, "/");
    const now = new Date(newStr);
    //获取月
    let month = now.getMonth() + 1;
    //获取日
    let day = now.getDate();
    return month + "月" + day + "日";
}

export function timeDiff(start, end) {
    let newStr = start.replace(/-/g, "/");
    //当前的日期年月日
    let currentDateStr = end.replace(/-/g, "/");
    //获取目标日期
    let targetMill = (new Date(newStr)).getTime();
    //当前的时间戳
    let currentMill = (new Date(currentDateStr)).getTime();
    //两个时间戳相差的毫秒数
    let diffrentTimeMill = currentMill - targetMill;

    let diffrentDay = Math.ceil((diffrentTimeMill / 86400000));
    return diffrentDay;
}

export function dateDiff(start, end) {
    //当前的日期年月日
    let endStr = end.replace(/-/g, "/");
    //获取目标日期
    let startMill = (new Date()).getTime();
    //当前的时间戳
    let endMill = (new Date(endStr)).getTime() + 86400000;
    //两个时间戳相差的毫秒数
    let diffrentTimeMill = endMill - startMill;
    return diffrentTimeMill;
}

export function activityCoundownNum(differeMill) {
    let day = Math.floor(differeMill / 86400000);
    day = day < 10 ? ("0" + day) : day;
    let dayRemainder = differeMill % 86400000;
    let hour = Math.floor(dayRemainder / 3600000);
    hour = hour < 10 ? ("0" + hour) : hour;
    let hourRemainder = dayRemainder % 3600000;
    let minute = Math.floor(hourRemainder / 60000);
    minute = minute < 10 ? ("0" + minute) : minute;
    let minuteRemainder = hourRemainder % 60000;
    let second = Math.floor(minuteRemainder / 1000);
    second = second < 10 ? ("0" + second) : second;
    return (day + "天" + hour + "时" + minute + "分" + second + "秒");
}

//日期的转换
export function getCurrentDate() {
    return dayjs().format("YYYY/MM/DD");
}

/**
 * 开始时间和结束时间与当前时间比较
 * @param {Object} start
 * @param {Object} end
 */
export function compareToDate(start) {
    if (start === undefined || start === null) {
        return true;
    }
    // 开始的时间戳
    let newStart = start.replace(/-/g, "/");
    let newStartMill = (new Date(newStart)).getTime();

    let currentMill = (new Date(getCurrentDate())).getTime();
    if (newStartMill >= currentMill) {
        return true;
    }
    return false;
}

/**
 * 开始时间和结束时间与当前时间比较
 * @param {Object} start
 * @param {Object} end
 */
export function compareTo(start) {
    if (start === undefined || start === null) {
        return 0;
    }
    // 开始的时间戳
    let newStart = start.replace(/-/g, "/");
    let newStartMill = (new Date(newStart)).getTime();

    let currentMill = (new Date(getCurrentDate())).getTime();
    if (newStartMill > currentMill) {
        return 1;
    } else if (newStartMill === currentMill) {
        return 0;
    } else {
        return -1;
    }

}


//日期的转换
export function activityDateSub(start, end) {
    let currentStart = start.replace(/-/g, ".");
    let currentEnd = end.replace(/-/g, ".");

    return currentStart.substring(0, 10) + "-" + currentEnd.substring(5, 10);
}


//日期的转换
export function abnormalDateSub(start, end) {
    if ((start === undefined || start === null) && end !== undefined && end !== null) {
        let currentEnd = end.replace(/-/g, ".");
        return currentEnd;
    }
    if ((end === undefined || end === null) && start !== undefined && start !== null) {
        let currentStart = start.replace(/-/g, ".");
        return currentStart;
    }
    if (start.length < 16 || end.length < 16) {
        return ""
    }
    let currentStart = start.replace(/-/g, ".");
    let currentEnd = end.replace(/-/g, ".");

    return currentStart + "-" + currentEnd;
}

/**
 * 根据分钟获取小时数和分钟数
 * @param num 
 * @param type 
 * @returns 
 */
export function getHourOrMinte(num, type) {
    if (!num) { return "" };

    const hour = Math.floor(num / 60);

    //获取余数
    const minte = num % 60;

    if (type === "hour") {
        return hour === 0 ? '00' : '' + hour;
    } else {
        return minte === 0 ? '00' : '' + minte;
    }
}